﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Models;

public partial class PersonalManager_Position : System.Web.UI.Page
{
    List<Branch> branchList = new BranchManager().GetAllBranch();
    List<Department> deptList = new DeptManager().GetAllDepartment();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            foreach (Branch item in branchList)
            {

                TreeNode tn = new TreeNode("&nbsp;&nbsp;" + item.BranchShortName);
                tn.Value = item.BranchId.ToString();
                tn.ImageUrl = "~/imges/dept_tree.jpg";
                this.tvDeparment.Nodes.Add(tn);
                InitTreeByList(tn);

            }
            List<Position> list = new PosManager().GetAllPos();
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
        }


    }



    private void InitTreeByList(TreeNode TN)
    {
        foreach (Department item in deptList)
        {
            if (item.ParentId.ToString() == TN.Value)
            {
                TreeNode tn = new TreeNode();
                tn.Text = "&nbsp;&nbsp;" + item.DeptName;
                tn.Value = item.DeptId.ToString();
                //tn.NavigateUrl = item.NodeURL;
                tn.ImageUrl = "~/imges/dept_tree_block.jpg";
                TN.ChildNodes.Add(tn);
                InitTreeByList(tn);
            }


        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Position pos = new Position();
        pos.PosName = this.txtSearch.Text.Trim();
        Department dep = new Department();
        dep.DeptId = Convert.ToInt32(tvDeparment.SelectedNode.Value);
        pos.Department = dep;
        new PosManager().AddPosition(pos);
        this.GridView1.DataSource = new PosManager().GetAllPos();
        this.GridView1.DataBind();
        this.txtSearch.Text = "";
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string posName = this.txtSearch.Text.Trim();
        this.GridView1.DataSource = new PosManager().GetPosByPosName(posName);
        this.GridView1.DataBind();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Session["DeptName"] =(GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("lbDeptName") as Label).Text;
        this.GridView1.EditIndex = e.NewEditIndex;
        this.GridView1.DataSource = new PosManager().GetAllPos();
        this.GridView1.DataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        this.GridView1.DataSource = new PosManager().GetAllPos();
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        new PosManager().DeletePos(id);
        this.GridView1.DataSource = new PosManager().GetAllPos();
        this.GridView1.DataBind();

    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Position pos = new Position();
        pos.PosId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        pos.PosName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim();
        string deptName = Session["DeptName"].ToString();
        Department dept = new Department();
        dept.DeptId = new DeptManager().GetIdByDeptName(deptName);
        dept.DeptName = deptName;
        pos.Department = dept;
        if (pos.PosName == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('岗位和部门都不能为空')</script>");
            return;
        }
       

        new PosManager().UpdatePos(pos);
        this.GridView1.EditIndex = -1;
        this.GridView1.DataSource = new PosManager().GetAllPos();
        this.GridView1.DataBind();

    }
}